/**
 * @param {number[][]} properties
 * @return {number}
 */
 var numberOfWeakCharacters = function(properties) {
  properties.sort((a,b) => {
    if(a[0] === b[0]){
      return a[1] - b[1]
    }
    return a[0] - b[0]
  })
  let ans = 0
  const n = properties.length
  const defense = new Array(n)
  defense[n-1] = properties[n-1][1]
  for(let i = n-2; i>=0; i--){
    const [x,y] = properties[i]
    defense[i] = Math.max(y, defense[i+1])
  }
  for(let i = 0; i<n-1; i++){
    const [x,y] = properties[i]
    let j = i+1
    while(j < n && properties[j][0] === x){
      j++
    }
    if(j < n && defense[j] > y){
      ans++
    }
  }
  return ans
};